using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using WorkFlowCore.BusinessDemo.Web.Data;
using WorkFlowCore.BusinessDemo.Web.Entities;
using WorkFlowCore.BusinessDemo.Web.Models;
using System.Linq;

namespace WorkFlowCore.BusinessDemo.Web.Controllers
{
    public class RoleController : BaseController
    {
        private readonly BusinessDemoDbContext _context;

        public RoleController(BusinessDemoDbContext context)
        {
            _context = context;
        }

        // 角色列表
        public IActionResult Index()
        {
            var roles = _context.Roles.ToList();
            return View(roles);
        }

        // 创建角色 - GET
        public IActionResult Create()
        {
            return View(new RoleCreateViewModel());
        }

        // 创建角色 - POST
        [HttpPost]
        [ValidateAntiForgeryToken]
        public IActionResult Create(RoleCreateViewModel viewModel)
        {
            // 检查编号是否已存在
            if (_context.Roles.Any(r => r.Code == viewModel.Code))
            {
                ModelState.AddModelError("Code", "角色编号已存在，请使用其他编号");
                return View(viewModel);
            }

            if (ModelState.IsValid)
            {
                var role = new Role 
                { 
                    Code = viewModel.Code,
                    Name = viewModel.Name 
                };
                _context.Add(role);
                _context.SaveChanges();
                return RedirectToAction(nameof(Index));
            }
            return View(viewModel);
        }

        // 编辑角色 - GET
        public IActionResult Edit(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            var role = _context.Roles.Find(id);
            if (role == null)
            {
                return NotFound();
            }
            var viewModel = new RoleEditViewModel
            {
                Id = role.Id,
                Code = role.Code,
                Name = role.Name
            };
            return View(viewModel);
        }

        // 编辑角色 - POST
        [HttpPost]
        [ValidateAntiForgeryToken]
        public IActionResult Edit(int id, RoleEditViewModel viewModel)
        {
            if (id != viewModel.Id)
            {
                return NotFound();
            }

            // 获取当前角色
            var role = _context.Roles.Find(id);
            if (role == null)
            {
                return NotFound();
            }

            // 检查编号是否被修改
            if (role.Code != viewModel.Code)
            {
                ModelState.AddModelError("Code", "角色编号不可修改");
                return View(viewModel);
            }

            if (ModelState.IsValid)
            {
                try
                {
                    role.Name = viewModel.Name;
                    _context.Update(role);
                    _context.SaveChanges();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!RoleExists(viewModel.Id))
                    {
                        return NotFound();
                    }
                    else
                    {
                        throw;
                    }
                }
                return RedirectToAction(nameof(Index));
            }
            return View(viewModel);
        }

        private bool RoleExists(int? id)
        {
            return _context.Roles.Any(e => e.Id == id);
        }
    }
}
